package com.sky.server.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.mapper.WorkspaceMapper;
import com.sky.server.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private WorkspaceMapper workspaceMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public DishOverViewVO getDishList() {
        int disCount = 0;
        int soldCount = 0;
        DishOverViewVO dishOverViewVO = new DishOverViewVO();


        disCount =  workspaceMapper.getDishList(0);//停售数量

        soldCount =  workspaceMapper.getDishList(1);//起售数量

        dishOverViewVO.setSold(soldCount);
        dishOverViewVO.setDiscontinued(disCount);
        log.info("==dishOverViewVO:{}",dishOverViewVO);

        return dishOverViewVO;
    }

    @Override
    public OrderOverViewVO getOrderData() {
        Integer allOrders = null;//订单状态码
        Integer WaitingOrders = 2;//待接单
        Integer deliveredOrders = 3;//待派送
        Integer completedOrders = 5;//已完成
        Integer cancelledOrders = 6;// 取消

        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        orderOverViewVO.setAllOrders(workspaceMapper.getOrderDataByStatus(allOrders));
        orderOverViewVO.setWaitingOrders(workspaceMapper.getOrderDataByStatus(WaitingOrders));
        orderOverViewVO.setDeliveredOrders(workspaceMapper.getOrderDataByStatus(deliveredOrders));
        orderOverViewVO.setCompletedOrders(workspaceMapper.getOrderDataByStatus(completedOrders));
        orderOverViewVO.setCancelledOrders(workspaceMapper.getOrderDataByStatus(cancelledOrders));






        return orderOverViewVO;
    }



    @Override
    public BusinessDataVO selectNowDay() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String date = dateTimeFormatter.format(LocalDateTime.now());

        log.info("date:{}",date);
        BusinessDataVO businessDataVO = new BusinessDataVO();
        List<Orders> list = orderMapper.activeOrder(date);//完成订单
        if (list.size() == 0) {
            businessDataVO.setUnitPrice(0.0);
            businessDataVO.setTurnover(0.0);
            businessDataVO.setNewUsers(0);
            businessDataVO.setOrderCompletionRate(0.0);
            businessDataVO.setValidOrderCount(0);
            return businessDataVO;
        }
        //营业额
        Double turnover = 0.0;
        for (Orders orders : list) {
            double v = orders.getAmount().doubleValue();
            turnover += v;
        }
        businessDataVO.setTurnover(turnover);
        //有效订单数
        int count = orderMapper.activeOrderCount(date);
        businessDataVO.setValidOrderCount(count);
        //订单完成率
        int allCount = orderMapper.allOrder(date);
        BigDecimal decimal = new BigDecimal(count);
        BigDecimal decimal2 = new BigDecimal(allCount);
        BigDecimal divide = decimal.divide(decimal2,2, RoundingMode.DOWN);
        Double d = divide.doubleValue();
        businessDataVO.setOrderCompletionRate(d);
        //平均客单价
        BigDecimal turnovers = new BigDecimal(turnover);
        BigDecimal counts = new BigDecimal(count);
        BigDecimal avg = turnovers.divide(counts, 2);
        double doubleValue = avg.doubleValue();
        businessDataVO.setUnitPrice(doubleValue);
        //新增用户
        int userCount = userMapper.count(date);
        businessDataVO.setNewUsers(userCount);
        return businessDataVO;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        SetmealOverViewVO setmeal = new SetmealOverViewVO();
        int sold = setmealMapper.launchedCount();
        int discontinuedCount = setmealMapper.discontinuedCount();
        setmeal.setDiscontinued(discontinuedCount);
        setmeal.setSold(sold);
        return setmeal;
    }
}
